/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.criteria;

import java.util.Collection;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;

/**
 * This class is the hibernate implementation of <code>CriteriaResult</code>
 * that represents a line of result from a criteria.
 * 
 * @param <MAINTYPE>
 *          The main type of the criteria that generated this result
 */
@SuppressWarnings("serial")
public class HibernateCriteriaResult<MAINTYPE>
    extends BasicCriteriaResult<MAINTYPE>
{

  /**
   * Constructor.
   * 
   * @param criteria
   *          the <code>HibernateCriteria</code> that generate this line of
   *          result
   * @param line
   *          the line of result
   */
  @SuppressWarnings("unchecked")
  public HibernateCriteriaResult(final HibernateCriteria<MAINTYPE> criteria, final Object... line)
  {
    super(criteria.getMainType());
    if (criteria.getProjections() == null || criteria.getProjections().isEmpty()) {
      fill((Collection) criteria.getBaseQuery(), line);
    }
    else {
      fill((Collection) criteria.getProjections().keySet(), line);
    }
  }



  /**
   * this method fill the <code>Map</code> with the line of result.
   * 
   * @param baseQuery
   *          the types of the result
   * @param line
   *          the result itself
   */
  private void fill(final Collection<HibernateCriteriaType<?>> baseQuery, final Object... line)
  {
    int index = 0;
    for (final HibernateCriteriaType<?> type : baseQuery) {
      put(type, line[index]);
      index++;
    }
  }

}
